package com.qianyu.project.core.utils;


import com.qianyu.project.core.bean.Flow;
import com.qianyu.project.core.exception.ModuleIdentityNotFoundException;
import com.qianyu.project.core.exception.FlowNotFoundException;
import com.qianyu.project.core.exception.NotImplementedException;
import com.qianyu.project.core.exception.PrimaryKeyNotFoundException;
import com.qianyu.project.core.context.handler.FlowFinishedHandler;

/**
 * @author gs
 * @date create in 2020/12/11 17:10
 */
public class Assert {

    /**
     * description: 流程是否为空 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [flow] <br/>
     * return: void
     */
    public static void flowNotNull(Flow flow) throws FlowNotFoundException {
        if (flow == null) {
            throw new FlowNotFoundException("The Flow does not exist or obsolete");
        }
    }

    /**
     * description: 注解是否为空 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [primaryKey] <br/>
     * return: void
     */
    public static void flowEntryNotNull(Object object) throws PrimaryKeyNotFoundException {
        if (object == null) {
            throw new PrimaryKeyNotFoundException("Can not found the flowEntry annotation from the method parameters");
        }
    }

    /**
     * description: 流程主键未找到异常 <br/>
     * author: gs   <br/>
     * date: 2021/1/4 <br/>
     * params: [object] <br/>
     * return: void
     */
    public static void moduleIdentityNotNull(Object object) throws ModuleIdentityNotFoundException {
        if(object == null){
            throw new ModuleIdentityNotFoundException("Can not found the module identity from the method return value");
        }
    }

    /**
     * description: 主键是否为空 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [primaryKey] <br/>
     * return: void
     */
    public static void primaryKeyNotNull(Object object) throws PrimaryKeyNotFoundException {
        if (object == null) {
            throw new PrimaryKeyNotFoundException("Can not found the flowEntry annotation from the method parameters");
        }
    }

    /**
     * description: 未实现FlowFinishHandler接口 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [type] <br/>
     * return: void
     */
    public static void notImplements(Class type) throws NotImplementedException {
        Class[] interfaces = type.getInterfaces();
        boolean check = false;
        for (Class clazz : interfaces) {
            if (clazz == FlowFinishedHandler.class) {
                check = true;
                break;
            }
        }
        if (!check) {
            StringBuilder builder = new StringBuilder("Please let ");
            builder.append(type.getPackage().getName()).append(".")
                    .append(type.getSimpleName())
                    .append(" to implements ")
                    .append(FlowFinishedHandler.class.getPackage().getName())
                    .append(".").append(FlowFinishedHandler.class.getSimpleName());
            throw new NotImplementedException(builder.toString());
        }
    }
}
